package com.person.jc.web.base.customer;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;
import com.person.jc.service.base.customer.BdCustomerService;
import com.person.jc.utils.Servlets;
import com.person.jc.utils.Utils;

public class BdCustomerController extends Controller {
	
	public void init(){
		setAttr("menuId", getPara("menuId"));
		render("main.jsp");
	}
	
	public void list(){
		Map<String,Object> map = Servlets.getParametersStartingWith(getRequest(), "p_");
		List<Record> list = BdCustomerService.me.getList(map);
		setAttr("list", list);
		render("list.jsp");
	}
	
	public void edit(){
		String custId = getPara("custId");
		String view = getPara("view");
		if(Utils.isNotEmpty(custId)){
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("custId", custId);
			List<Record> list = BdCustomerService.me.getList(map);
			if(list.size() != 0){
				setAttr("obj", list.get(0));
			}
		}
		setAttr("view", view);
		render("edit.jsp");
	}
	
	public void save(){
		String res = "success";
		Map<String,Object> map = Servlets.getParametersStartingWith(getRequest(), "p_");
		String custId = String.valueOf(map.get("custId"));
		if(Utils.isNotEmpty(custId)){
			BdCustomerService.me.update(map);
		}else{
			BdCustomerService.me.save(map);
		}
		renderText(res);
	}
	
	public void delete(){
		String res = "success";
		String custIds = getPara("custIds");
		for (String custId : custIds.split(",")) {
			BdCustomerService.me.delete(custId);
		}
		renderText(res);
	}
	
	public void checkName(){
		String custCode = getPara("custCode");
		String custId = getPara("custId");
		String custName = getPara("custName");
		long count = BdCustomerService.me.checkName(custCode, custId, custName);
		renderText(count == 0 ? "success" : "error");
	}
}
